/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-08-14 10:08:57
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Mars3d\Initialization.jsx
 * @Description  :
 */

import { map3dConfig } from './Map'
import { RenderElements, RenderFiles } from './Render'
const { mars3d } = window

// 初始化场景函数
export const Initialization = (scene, MapDiv, InitializationEvent, additional) => {
    const { basemaps, layers, center, uuid } = scene
    const configure = { ...map3dConfig, ...additional }
    // 初始化地图
    const map = new mars3d.Map(MapDiv.current, configure)
    const gra = new mars3d.layer.GraphicLayer()
    InitializeBasemaps(map, basemaps)
    InitializeLayers(map, layers)
    InitializeCenter(map, center)
    map.addLayer(gra)
    InitializationEvent.emit({ map, gra, uuid })
}

// 初始化底图
const InitializeBasemaps = (map, data) => {
    new mars3d.layer.MapboxLayer({ maximumLevel: 16 })
    const basemapsObj = map.getLayers({ basemaps: true })
    if (data !== null) map.addLayer(basemapsObj.find(v => v.id === Number(data)))
    else map.addLayer(basemapsObj[0])
}
// 初始化辅助图层
const InitializeLayers = (map, data) => {
    const layersObj = map.getLayers({ layers: true })
    if (data !== null && data !== undefined && data !== '[]') {
        data = JSON.parse(data)
        data.forEach(key => map.addLayer(layersObj.find(v => v.id === key)))
    }
}
// 初始化视角
export const InitializeCenter = (map, center) => {
    if (typeof center !== 'string') center = JSON.stringify(map3dConfig.scene.center)
    center = JSON.parse(center)
    map.centerAt(center)
}

// 初始化数据
export const InitializationData = (map, gra, { elements, files }, status) => {
    if (elements && elements.length !== 0) RenderElements(map, gra, elements, status)
    if (files && files.length !== 0) RenderFiles(map, files, elements)
}
